/**
 * Copyright (c) 2013 An Yaming,  All Rights Reserved
 */
package so.shanku.essential.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.*;
import android.widget.AdapterView.OnItemSelectedListener;
import net.tsz.afinal.json.JsonMap;
import net.tsz.afinal.json.JsonParseHelper;
import net.tsz.afinal.annotation.view.ViewInject;
import so.shanku.essential.R;
import so.shanku.essential.activity.base.BaseUIActivity;
import so.shanku.essential.getdata.*;
import so.shanku.essential.utils.ExtraKeys;
import so.shanku.essential.utils.StringUtil;
import so.shanku.essential.utils.Utils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 新建联系人地址
 *
 * @author admin
 */
public class UserRecipient_New_Activity extends BaseUIActivity {
    /**
     * 收货人输入框
     */
    @ViewInject(id = R.id.u_n_a_et_recipient)
    private EditText et_recipient;
    /**
     * 手机输入框
     */
    @ViewInject(id = R.id.u_n_a_et_phone)
    private EditText et_phone;

    /**
     * 添加 号输入框
     */
    @ViewInject(id = R.id.u_n_a_et_addfetch)
    private EditText et_fetch;
    /**
     * 添加 楼/室输入框
     */
    @ViewInject(id = R.id.u_n_a_et_addfloor)
    private EditText et_floor;
    /**
     * 地址显示
     */
    // @ViewInject(id = R.id.u_n_a_tv_address)
    // private TextView tv_address;
    /**
     * 地址输入框
     */
    @ViewInject(id = R.id.u_n_a_et_address)
    private EditText et_address;
    /**
     * 邮编输入框
     */
    @ViewInject(id = R.id.u_n_a_et_youbian)
    private EditText et_youbian;
    /**
     * 确认按钮
     */
    @ViewInject(id = R.id.u_n_a_ib_config, click = "addNewAddress")
    private Button u_n_a_ib_recipient;
    /**
     * 省选择器
     */
    @ViewInject(id = R.id.u_n_a_sp_city)
    private Spinner s_sheng;
    /**
     * 省的数据源
     */
    private List<JsonMap<String, Object>> data_sheng;
    /**
     * 市选择器
     */
    @ViewInject(id = R.id.u_n_a_sp_city1)
    private Spinner s_shi;
    /**
     * 市的数据源
     */
    private List<JsonMap<String, Object>> data_shi;
    /**
     * 县选择器
     */
    @ViewInject(id = R.id.u_n_a_sp_city3)
    private Spinner s_xian;
    /**
     * 县的数据源
     */
    private List<JsonMap<String, Object>> data_xian;
    /**
     * 镇选择器
     */
    @ViewInject(id = R.id.u_n_a_sp_city4)
    private Spinner s_zhen;
    /**
     * 镇的数据源
     */
    private List<JsonMap<String, Object>> data_zhen;

    /**
     * 有上个界面传入的地址信息的id 为空时则为新建
     */
    private String address_id;
    private String districtid;
    private String cityid;
    /**
     * 当是修改是修改前的信息
     */
    private JsonMap<String, Object> info;
    /**
     * 是否加载过地址信息
     */
    private boolean isLoaded = false;
    /**
     * 省是否进行过默认选择
     */
    private boolean noSelectSheng = true;
    /**
     * 市是否进行过默认选择
     */
    private boolean noSelectShi = true;
    /**
     * 县是否进行过默认选择
     */
    private boolean noSelectXian = true;
    /**
     * 是否是第一次选中县 并且是在isLoaded=true
     */
    private boolean isFirstSelectXian = true;
    /**
     * 镇是否进行过默认选择
     */
    private boolean noSelectZhen = true;
    /**
     * 是否是第一次选中镇并且是在isLoaded=true
     */
    private boolean isFirstSelectZhen = true;
    /**
     * 是否为默认地址
     */
    private boolean isDefault = false;
    /**
     * 默认地址布局
     */
    @ViewInject(id = R.id.rl_address_default)
    private RelativeLayout rl_Isdefault;
    /**
     * 默认地址按钮
     */
    @ViewInject(id = R.id.iv_address_default, click = "Isdefault")
    private ImageView btn_Isdefault;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_new_address);
        Intent intent = getIntent();
        address_id = intent.getStringExtra(ExtraKeys.Key_Msg1);
        if (address_id != null) {
            districtid = intent.getStringExtra(ExtraKeys.Key_Msg2);
            cityid = intent.getStringExtra(ExtraKeys.Key_Msg3);
            Log.i("***", "districtid=" + districtid);
        }

        if (TextUtils.isEmpty(address_id)) {
//			getData_userInfo();
            setCenter_title(R.string.u_n_a_title);
            rl_Isdefault.setVisibility(View.GONE);

        } else {
            setCenter_title(R.string.u_n_a_title_update);
        }
        isLoaded = false;
        noSelectSheng = true;
        noSelectShi = true;
        noSelectXian = true;
        isFirstSelectXian = true;
        noSelectZhen = true;
        isFirstSelectZhen = true;

        getData_sheng();
        s_sheng.setOnItemSelectedListener(itemSelectedListener);
        s_shi.setOnItemSelectedListener(itemSelectedListener);
        s_xian.setOnItemSelectedListener(itemSelectedListener);
        s_zhen.setOnItemSelectedListener(itemSelectedListener);
//		et_address.addTextChangedListener(editTextContentChangeListener);
//		et_fetch.addTextChangedListener(editTextContentChangeListener);
//		et_floor.addTextChangedListener(editTextContentChangeListener);
//		et_phone.addTextChangedListener(editTextContentChangeListener);
//		et_recipient.addTextChangedListener(editTextContentChangeListener);
//		et_youbian.addTextChangedListener(editTextContentChangeListener);
    }

    /**
     * 刷新获取到的信息
     */
    private void flushAddressInfo(JsonMap<String, Object> info) {
        this.info = info;
        et_recipient.setText(info.getStringNoNull("consignee"));
        et_phone.setText(info.getStringNoNull("phone"));
        et_fetch.setText(info.getStringNoNull("addressDetailP1"));
        et_floor.setText(info.getStringNoNull("addressDetailP2"));
        et_address.setText(info.getStringNoNull("detail"));
        et_youbian.setText(info.getStringNoNull("postCode"));
        // tv_address.setText(info.getStringNoNull("consigneeAddress"));

        if (info.getBoolean("isDefault", true)) {
            btn_Isdefault.setImageResource(R.drawable.address_default_on);
        } else {
            btn_Isdefault.setImageResource(R.drawable.address_default_off);
        }

        if (isLoaded && noSelectSheng && null != info) {
            int sheng = info.getInt("province");
            int p = 0;
            for (int i = 0; i < data_sheng.size(); i++) {
                if (sheng == data_sheng.get(i).getInt("ProvinceID")) {
                    p = i;
                    break;
                }
            }
            if (p != 0) {
                s_sheng.setSelection(p);
            }
            noSelectSheng = false;
        }
        if (isLoaded && noSelectShi && null != info) {
            int sheng = info.getInt("city");
            int p = 0;
            for (int i = 0; i < data_shi.size(); i++) {
                if (sheng == data_shi.get(i).getInt("CityID")) {
                    p = i;
                    break;
                }
            }
            if (p != 0) {
                s_shi.setSelection(p);
            }
            noSelectShi = false;
        }
        if (isLoaded && noSelectXian && null != info) {
            int sheng = info.getInt("district");
            int p = 0;
            for (int i = 0; i < data_xian.size(); i++) {
                if (sheng == data_xian.get(i).getInt("DistrictID")) {
                    p = i;
                    break;
                }
            }
            if (p != 0) {
                s_xian.setSelection(p);
            }
            noSelectXian = false;
        }
        if (isLoaded && noSelectZhen && null != info) {
            int sheng = info.getInt("subDistrictId");
            // if(sheng!=0){
            if (data_zhen != null) {
                if (data_zhen.size() != 0) {
                    s_zhen.setVisibility(View.GONE);
                    int p = 0;
                    for (int i = 0; i < data_zhen.size(); i++) {
                        if (sheng == data_zhen.get(i).getInt("Id")) {
                            p = i;
                            break;
                        }
                    }
                    if (p != 0) {
                        s_zhen.setSelection(p);
                    }
                    noSelectZhen = false;
                } else {
                    s_zhen.setVisibility(View.GONE);
                }
            }

            // }else{
            // s_zhen.setVisibility(View.GONE);
            // }

        }
    }

    /**
     * 为省份设定数据适配
     */
    private void setAdapter_sheng(List<JsonMap<String, Object>> data) {
        this.data_sheng = data;
        // BaseAdapter adapter = new HasClickAdapter(this, data,
        // R.layout.item_list_textview, new String[] { "ProvinceName" },
        // new int[] { R.id.i_l_tv_name }, 0);

        // 修改显示方式
        String[] sheng = new String[data_sheng.size()];
        for (int i = 0; i < data_sheng.size(); i++) {
            sheng[i] = data_sheng.get(i).getStringNoNull("ProvinceName");
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, sheng);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s_sheng.setPromptId(R.string.u_n_a_tv_text_select_province);
        // end
        s_sheng.setAdapter(adapter);

    }

    /**
     * 为市设定数据适配
     */
    private void setAdapter_shi(List<JsonMap<String, Object>> data) {
        this.data_shi = data;
        // BaseAdapter adapter = new HasClickAdapter(this, data,
        // R.layout.item_list_textview, new String[] { "CityName" },
        // new int[] { R.id.i_l_tv_name }, 0);
        // 修改显示方式
        String[] shi = new String[data_shi.size()];
        for (int i = 0; i < data_shi.size(); i++) {
            shi[i] = data_shi.get(i).getStringNoNull("CityName");
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, shi);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s_shi.setPromptId(R.string.u_n_a_tv_text_select_city);
        // end
        s_shi.setAdapter(adapter);
        if (address_id != null) {

            int sheng = Integer.parseInt(cityid);
            int p = 0;
            for (int i = 0; i < data_shi.size(); i++) {
                if (sheng == data_shi.get(i).getInt("CityID")) {
                    p = i;
                    break;
                }
            }
            if (p != 0) {
                s_shi.setSelection(p);
            }
            noSelectShi = false;

        }
        if (isLoaded && noSelectShi && null != info) {
            int sheng = info.getInt("city");
            int p = 0;
            for (int i = 0; i < data_shi.size(); i++) {
                if (sheng == data_shi.get(i).getInt("CityID")) {
                    p = i;
                    break;
                }
            }
            if (p != 0) {
                s_shi.setSelection(p);
            }
            noSelectShi = false;
        }

    }

    /**
     * 为县设定数据适配
     */
    private void setAdapter_xian(List<JsonMap<String, Object>> data) {
        this.data_xian = data;

        // s_xian.setBackgroundResource(R.drawable.user_address_duan);
        // BaseAdapter adapter = new HasClickAdapter(this, data,
        // R.layout.item_list_textview, new String[] { "DistrictName" },
        // new int[] { R.id.i_l_tv_name }, 0);

        // 修改显示方式
        String[] xian = new String[data_xian.size()];
        for (int i = 0; i < data_xian.size(); i++) {
            xian[i] = data_xian.get(i).getStringNoNull("DistrictName");
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, xian);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s_xian.setPromptId(R.string.u_n_a_tv_text_select_district);
        s_xian.setAdapter(adapter);
        // end
        if (address_id != null) {

            int sheng = Integer.parseInt(districtid);
            int p = 0;
            for (int i = 0; i < data_xian.size(); i++) {
                if (sheng == data_xian.get(i).getInt("DistrictID")) {
                    p = i;
                    break;
                }
            }
            if (p != 0) {
                s_xian.setSelection(p);
            }
            noSelectXian = false;

        }
        if (isLoaded && noSelectXian && null != info) {
            int sheng = info.getInt("district");
            int p = 0;
            for (int i = 0; i < data_xian.size(); i++) {
                if (sheng == data_xian.get(i).getInt("DistrictID")) {
                    p = i;
                    break;
                }
            }
            if (p != 0) {
                s_xian.setSelection(p);
            }
            noSelectXian = false;
        }

    }

    /**
     * 为镇设定数据适配
     */
    private void setAdapter_zhen(List<JsonMap<String, Object>> data) {
        this.data_zhen = data;
        if (data_zhen.size() != 0) {
            s_zhen.setVisibility(View.GONE);
            // 修改显示方式
            String[] zhen = new String[data_zhen.size()];
            for (int i = 0; i < data_zhen.size(); i++) {
                zhen[i] = data_zhen.get(i).getStringNoNull("Name");
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    // android.R.layout.simple_spinner_item, zhen);
                    R.layout.address_spinner_item, zhen);
            // adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adapter.setDropDownViewResource(R.layout.item_spinner_zhen);
            s_zhen.setPromptId(R.string.u_n_a_tv_text_select_district);
            s_zhen.setAdapter(adapter);
            // end
            if (isLoaded && noSelectZhen && null != info) {
                int sheng = info.getInt("subDistrictId");
                int p = 0;
                for (int i = 0; i < data_zhen.size(); i++) {
                    if (sheng == data_zhen.get(i).getInt("Id")) {
                        p = i;
                        break;
                    }
                }
                if (p != 0) {
                    s_zhen.setSelection(p);
                }
                noSelectZhen = false;
            }
        } else {
            s_zhen.setVisibility(View.GONE);
        }
        // s_xian.setBackgroundResource(R.drawable.user_address_duan);
        // BaseAdapter adapter = new HasClickAdapter(this, data,
        // R.layout.item_list_textview, new String[] { "DistrictName" },
        // new int[] { R.id.i_l_tv_name }, 0);

    }

    /**
     * 确认默认地址
     *
     * @param v
     */
    public void Isdefault(View v) {
        if (isDefault) {
            btn_Isdefault.setImageResource(R.drawable.address_default_off);
            isDefault = false;
        } else {
            btn_Isdefault.setImageResource(R.drawable.address_default_on);
            isDefault = true;
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("AddressId", address_id);
        params.put("IsDefault", isDefault);

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_GetAddressIsDefault);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(what_isdefault);
        getDataUtil.getData(queue);
    }

    /**
     * 确认按钮事件
     *
     * @param v
     */
    public void addNewAddress(View v) {
        String shouhuaren = et_recipient.getText().toString();
        String dianhua = et_phone.getText().toString();
        String hao = et_fetch.getText().toString();
        String loushi = et_floor.getText().toString();
        String dizhi = et_address.getText().toString();
        String youbian = et_youbian.getText().toString();
        // String dizhi_1 = tv_address.getText().toString();
        // if (dizhi.startsWith(dizhi_1)) {
        // dizhi = dizhi.substring(dizhi_1.length());
        // }
        if (TextUtils.isEmpty(shouhuaren) || TextUtils.isEmpty(dianhua)
                || TextUtils.isEmpty(dizhi)) {
            toast.showToast(R.string.u_n_a_alerm_infonull);
            return;
        }
        if (!StringUtil.isMobileNO(dianhua)) {
            toast.showToast(R.string.phone_formal_error);
            return;
        }
        if (Utils.isFastClick()) {
            return;
        }
        if (TextUtils.isEmpty(address_id)) {
            getData_saveAddressInfo(shouhuaren, dianhua, hao, loushi, dizhi,
                    youbian);
        } else {
            getData_updataAddressInfo(shouhuaren, dianhua, hao, loushi, dizhi,
                    youbian);
        }
    }

    /**
     * 根据地址id获取地址信息
     */
    private void getData_getAddressInfo() {
        //loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("Id", address_id);

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_userAddressById);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(what_getInfo);
        getDataUtil.getData(queue);

    }

    /**
     * 修改地址信息
     */
    private void getData_updataAddressInfo(String consignee, String phone,
                                           String hao, String loushi, String detail, String postCode) {
        loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("consignee", consignee);
        params.put("phone", phone);
        params.put("addressDetailP1", hao);
        params.put("addressDetailP2", loushi);
        params.put("province",
                data_sheng.get(s_sheng.getSelectedItemPosition())
                        .getStringNoNull("ProvinceID"));
        params.put("city", data_shi.get(s_shi.getSelectedItemPosition())
                .getStringNoNull("CityID"));
        params.put("district", data_xian.get(s_xian.getSelectedItemPosition())
                .getStringNoNull("DistrictID"));
        // params.put("district",
        // data_zhen.get(s_zhen.getSelectedItemPosition())
        // .getStringNoNull("DistrictID"));
        if (data_zhen != null) {
            if (data_zhen.size() != 0) {
                params.put("subDistrictId",
                        data_zhen.get(s_zhen.getSelectedItemPosition())
                                .getStringNoNull("Id"));
            }
        } else {
            params.put("subDistrictId", 0);
        }
        params.put("detail", detail);
        params.put("postCode", postCode);
        params.put("userName", Utils.getUserAccount(this));
        params.put("id", address_id);

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_userUpdateAddressById);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(what_updateInfo);
        getDataUtil.getData(queue);

    }

    /**
     * 保存地址信息
     */
    private void getData_saveAddressInfo(String consignee, String phone,
                                         String hao, String loushi, String detail, String postCode) {
        loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("consignee", consignee);
        params.put("phone", phone);
        params.put("addressDetailP1", hao);
        params.put("addressDetailP2", loushi);
        params.put("province",
                data_sheng.get(s_sheng.getSelectedItemPosition())
                        .getStringNoNull("ProvinceID"));
        params.put("city", data_shi.get(s_shi.getSelectedItemPosition())
                .getStringNoNull("CityID"));
        params.put("district", data_xian.get(s_xian.getSelectedItemPosition())
                .getStringNoNull("DistrictID"));
        if (data_zhen != null) {
            if (data_zhen.size() != 0) {
                params.put("subDistrictId",
                        data_zhen.get(s_zhen.getSelectedItemPosition())
                                .getStringNoNull("Id"));
            }
        } else {
            params.put("subDistrictId", 0);
        }
        params.put("detail", detail);
        params.put("postCode", postCode);
        params.put("userName", Utils.getUserAccount(this));

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_userInserAddress);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(what_saveInfo);
        getDataUtil.getData(queue);

    }

    /**
     * 查询可配送的省
     */
    private void getData_sheng() {
        loadingToast.show();

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_userGetProvince);
        queue.setParams(null);
        queue.setCallBack(callBack);
        queue.setWhat(what_sheng);
        getDataUtil.getData(queue);
    }

    /**
     * 查询可配送的市
     */
    private void getData_shi(String provinceId) {
//		loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("provinceId", provinceId);

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_userGetCityByProvinceID);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(what_shi);
        getDataUtil.getData(queue);
    }

    /**
     * 查询可配送的县
     */
    private void getData_xian(String cityId) {
//		loadingToast.show();

        Map<String, Object> params = new HashMap<String, Object>();
        params.put("cityId", cityId);

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_userGetDistrictByCityID);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(what_xian);
        getDataUtil.getData(queue);
    }

    /**
     * 查询可配送的镇
     */
    private void getData_zhen(String districtId) {
//		loadingToast.show();

        Map<String, Object> params = new HashMap<String, Object>();
        // params.put("DistrictID", districtId);
        params.put("districtId", districtId);

        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_SubDistrictByDistrictId);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(what_zhen);
        getDataUtil.getData(queue);
    }

    /**
     * 获得用户的信息
     */
    private void getData_userInfo() {
        loadingToast.show();
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userName", Utils.getUserAccount(this));
        GetDataQueue queue = new GetDataQueue();
        queue.setActionName(GetDataConfing.Action_userGetUserInfoByuserName);
        queue.setParamsNoJson(params);
        queue.setCallBack(callBack);
        queue.setWhat(what_getUserInfo);
        getDataUtil.getData(queue);
    }

    private final int what_getInfo = 1;
    private final int what_saveInfo = 2;
    private final int what_updateInfo = 3;
    private final int what_sheng = 4;
    private final int what_shi = 5;
    private final int what_xian = 6;
    private final int what_zhen = 7;
    private final int what_isdefault = 8;
    private final int what_getUserInfo = 9;
    /**
     * 获取服务器信息的返回
     */
    private IGetServicesDataCallBack callBack = new IGetServicesDataCallBack() {

        @Override
        public void onLoaded(GetDataQueue entity) {
            if (entity.isOk()) {
                JsonMap<String, Object> data = JsonParseHelper
                        .getJsonMap(entity.getInfo());
                /**
                 * if("无相关数据！".equals(data.getString(JsonKeys.Key_Msg))){ if
                 * (entity.what == what_zhen) { s_zhen.setVisibility(View.GONE);
                 * getData_getAddressInfo(); if(data_zhen!=null){
                 * data_zhen.clear();
                 *
                 * }
                 *
                 * } }
                 */
                if (entity.what == what_zhen) {
                    if (data.getInt(JsonKeys.Key_Code) == 1) {
                        s_zhen.setVisibility(View.GONE);
                        if (!isLoaded && null == info
                                && !TextUtils.isEmpty(address_id)) {
                            getData_getAddressInfo();
                        }
                        if (data_zhen != null) {
                            data_zhen.clear();
                        }
                    } else if (data.getInt(JsonKeys.Key_Code) == 0) {
                        setAdapter_zhen(JsonParseHelper
                                .getJsonMap_List_JsonMap(entity.getInfo(),
                                        JsonKeys.Key_Info));
                        if (entity.getInfo() == null) {
                            s_zhen.setVisibility(View.GONE);
                        }
                    } else {
                        ShowGetDataError.isOkAndCodeIsNot1(
                                UserRecipient_New_Activity.this,
                                entity.getInfo());
                    }
                } else if (ShowGetDataError.isOkAndCodeIsNot1(
                        UserRecipient_New_Activity.this,
                        entity.getInfo())) {
                    if (entity.what == what_getInfo) {
                        //loadingToast.dismiss();
                        List<JsonMap<String, Object>> info = JsonParseHelper
                                .getJsonMap_List_JsonMap(entity.getInfo(),
                                        JsonKeys.Key_Info);
                        if (info.size() > 0) {
                            isLoaded = true;
                            flushAddressInfo(info.get(0));
                        }

                    } else if (entity.what == what_saveInfo) {
                        loadingToast.dismiss();
                        JsonMap jsonMap = JsonParseHelper.getJsonMap_List_JsonMap(entity.getInfo(), JsonKeys.Key_Info).get(0);
                        toast.showToast(JsonParseHelper.getAttribute(
                                entity.getInfo(),
                                new String[]{JsonKeys.Key_Msg}));

                        String addressStr = data_sheng.get(s_sheng.getSelectedItemPosition()).getStringNoNull("ProvinceName") +
                                data_shi.get(s_shi.getSelectedItemPosition()).getStringNoNull("CityName") +
                                data_xian.get(s_xian.getSelectedItemPosition()).getStringNoNull("DistrictName") +
                                //data_zhen.get(s_zhen.getSelectedItemPosition()).getStringNoNull("Name") +
                                et_address.getText().toString();
                        String userName = et_recipient.getText().toString();
                        String userPhone = et_phone.getText().toString();
                        String userAddressId = jsonMap.getStringNoNull("One");//地址对应的id
                        Intent resultIntent = new Intent();
                        resultIntent.putExtra(ExtraKeys.Key_Msg1, userName);
                        resultIntent.putExtra(ExtraKeys.Key_Msg2, userPhone);
                        resultIntent.putExtra(ExtraKeys.Key_Msg3, addressStr);
                        resultIntent.putExtra(ExtraKeys.Key_Msg4, userAddressId);
                        setResult(Activity.RESULT_OK, resultIntent);
                        mContext.finish();
                    } else if (entity.what == what_updateInfo) {
                        loadingToast.dismiss();
                        toast.showToast(JsonParseHelper.getAttribute(
                                entity.getInfo(),
                                new String[]{JsonKeys.Key_Msg}));
                        Intent resultIntent = new Intent();
                        setResult(Activity.RESULT_OK, resultIntent);
                        mContext.finish();
                    } else if (what_sheng == entity.what) {
                        setAdapter_sheng(JsonParseHelper
                                .getJsonMap_List_JsonMap(entity.getInfo(),
                                        JsonKeys.Key_Info));
                    } else if (what_shi == entity.what) {
                        setAdapter_shi(JsonParseHelper.getJsonMap_List_JsonMap(
                                entity.getInfo(), JsonKeys.Key_Info));
                    } else if (what_xian == entity.what) {
                        loadingToast.dismiss();
                        setAdapter_xian(JsonParseHelper
                                .getJsonMap_List_JsonMap(entity.getInfo(),
                                        JsonKeys.Key_Info));
                    } else if (what_isdefault == entity.what) {
                        loadingToast.dismiss();
                        toast.showToast(JsonParseHelper.getAttribute(
                                entity.getInfo(),
                                new String[]{JsonKeys.Key_Msg}));
                        setResult(RESULT_OK);
                    } else if (entity.what == what_getUserInfo) {
                        List<JsonMap<String, Object>> list = JsonParseHelper
                                .getJsonMap_List_JsonMap(entity.getInfo(),
                                        JsonKeys.Key_Info);
                        if (list.size() == 0) {
                            ShowGetDataError
                                    .showNetError(UserRecipient_New_Activity.this);
                        } else {
                            loadingToast.dismiss();
                            JsonMap<String, Object> info = list.get(0);
                            et_phone.setText(info.getStringNoNull("phone"));
                        }
                    }
                    /**
                     * else if (what_zhen == entity.what) {
                     * setAdapter_zhen(JsonParseHelper
                     * .getJsonMap_List_JsonMap(entity.getInfo(),
                     * JsonKeys.Key_Info)); if(entity.getInfo()==null){
                     * s_zhen.setVisibility(View.GONE); } //
                     * List<JsonMap<String, Object>> data=null; //
                     * setAdapter_zhen(data); }
                     */
                }
            } else {

                ShowGetDataError
                        .showNetError(UserRecipient_New_Activity.this);
            }
//			loadingToast.dismiss();
        }
    };
    /**
     * 省市县镇的选中事件
     */
    private OnItemSelectedListener itemSelectedListener = new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view,
                                   int position, long id) {
            if (parent.equals(s_sheng)) {
                JsonMap<String, Object> s = data_sheng.get(position);
                getData_shi(s.getStringNoNull("ProvinceID"));

            } else if (parent.equals(s_shi)) {
                if (address_id != null && !isLoaded) {
                    // JsonMap<String, Object> s = data_xian.get(position);
                    // getData_zhen(s.getStringNoNull("DistrictID"));
                    getData_xian(cityid);
                } else {
                    JsonMap<String, Object> s = data_shi.get(position);
                    getData_xian(s.getStringNoNull("CityID"));
                }

            } else if (parent.equals(s_xian)) {
                if (address_id != null && !isLoaded) {
                    // JsonMap<String, Object> s = data_xian.get(position);
                    // getData_zhen(s.getStringNoNull("DistrictID"));
                    getData_zhen(districtid);

                } else {
                    JsonMap<String, Object> s = data_xian.get(position);
                    getData_zhen(s.getStringNoNull("DistrictID"));
                }
                // JsonMap<String, Object> s = data_shi.get(position);
                // getData_zhen(s.getStringNoNull("CityID"));
                /**
                 * if (!isLoaded && null == info &&
                 * !TextUtils.isEmpty(address_id)) { getData_getAddressInfo(); }
                 * else { JsonMap<String, Object> sheng = data_sheng.get(s_sheng
                 * .getSelectedItemPosition()); JsonMap<String, Object> shi =
                 * data_shi.get(s_shi .getSelectedItemPosition());
                 * JsonMap<String, Object> xian = data_xian.get(s_xian
                 * .getSelectedItemPosition()); String add =
                 * sheng.getStringNoNull("ProvinceName") +
                 * shi.getStringNoNull("CityName") +
                 * xian.getStringNoNull("DistrictName"); //
                 * tv_address.setText(add); if (isLoaded && isFirstSelectXian &&
                 * null != info && !TextUtils.isEmpty(address_id)) {
                 * isFirstSelectXian = false; // loadingToast.dismiss(); } else
                 * { // et_address.setText(add); }
                 *
                 * }
                 */
            } else if (parent.equals(s_zhen)) {
                if (!isLoaded && null == info && !TextUtils.isEmpty(address_id)) {
                    getData_getAddressInfo();
                } else {
                    if (data_zhen.size() != 0) {
                        JsonMap<String, Object> sheng = data_sheng.get(s_sheng
                                .getSelectedItemPosition());
                        JsonMap<String, Object> shi = data_shi.get(s_shi
                                .getSelectedItemPosition());
                        JsonMap<String, Object> xian = data_xian.get(s_xian
                                .getSelectedItemPosition());
                        JsonMap<String, Object> zhen = data_zhen.get(s_zhen
                                .getSelectedItemPosition());
                        String add = sheng.getStringNoNull("ProvinceName")
                                + shi.getStringNoNull("CityName")
                                + xian.getStringNoNull("DistrictName")
                                + zhen.getStringNoNull("Name");
                        // tv_address.setText(add);
                        if (isLoaded && isFirstSelectZhen && null != info
                                && !TextUtils.isEmpty(address_id)) {
                            isFirstSelectZhen = false;
                            // loadingToast.dismiss();
                        } else {
                            // et_address.setText(add);
                        }
                    }

                }
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    };
}
